<?php
namespace ExAdmin\hyperf\middleware;

use Hyperf\HttpMessage\Stream\SwooleStream;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class CoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware
{
    protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
    {
        if($response instanceof \JsonSerializable){
            
            return $this->response()
                
                ->withAddedHeader('content-type', 'application/json')
                ->withBody(new SwooleStream(json_encode($response)));
        }
        return parent::transferToResponse($response, $request); // TODO: Change the autogenerated stub
    }
}